package org.tity.test.entity;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.tity.sql.dao.DAO;
import org.tity.test.entity.raw.hr.Person;
import org.tity.test.entity.raw.hr.PersonSet;
import org.tity.test.entity.raw.sys.Dict;
import org.tity.test.entity.raw.sys.DictItem;
import org.tity.test.entity.raw.sys.DictItemSet;
import org.tity.test.entity.raw.sys.DictSet;
import org.tity.test.entity.raw.sys.Role;
import org.tity.test.entity.raw.sys.RoleSet;
import org.tity.test.entity.raw.sys.User;
import org.tity.test.entity.raw.sys.UserSet;

@RestController
@RequestMapping(value="/entity")
public class EntityController {

	@Autowired
	private DAO dao;
	
	public Object getRandomElement(Object[] arr)
	{
		if(arr==null) return null;
		if(arr.length==0) return null;
		int i=-1;
		while(true)
		{
			i=(int)Math.round(Math.random()*arr.length);
			if(i>=0 && i<arr.length)	break;
		}
		return arr[i];
	}
	
	@RequestMapping(value="/start")
	public String start() {
		
		//更新关联的数据
		String[] areas= dao.query("select ID from sys_dict_item where dict_id=?","ADV_RESERVE_AREA").getValueArray("ID",String.class);
		String[] cards= dao.query("select ID from sys_dict_item where dict_id=?","CARD_TYPE").getValueArray("ID",String.class);
		String[] names= dao.query("select name from tmp_name").getValueArray("name",String.class);
		
		ArrayList<Object[]> ps=new ArrayList<Object[]>();
		
		//查询并转实体集合
		UserSet users=dao.query("select * from sys_user where area_type is null or card_type is null").toEntitySet(UserSet.class);
		for (User user : users) {
			System.out.println(user.getId()+","+user.getAccount());
			ps.add(new Object[] {getRandomElement(areas),getRandomElement(cards),user.getId()});
//			user.record().get
//			user
		}
		
		dao.batchExecute("update sys_user set area_type=? , card_type=? where id=?",ps);
		
		//简单的记录集转实体，并准备数据
		ps=new ArrayList<Object[]>();
		users=dao.query("select * from sys_user a where not exists(select 1 from hr_person b where a.id=b.id)").toEntitySet(UserSet.class);
		for (User user : users) {
			System.out.println(user.getId()+","+user.getAccount());
			ps.add(new Object[] {user.getId(),getRandomElement(names)});
		}
		dao.batchExecute("insert into hr_person(id,name) values(?,?)",ps);
		
		
		//一对一关联，与一对一的反向引用
		users=dao.query("select * from sys_user where account like ? order by id asc","%na%").toEntitySet(UserSet.class);
		//获得User关联的Person
		PersonSet persons=users.joinPerson();
		for (Person psn : persons) { //直接循环person
			 System.out.println("from list "+psn.getName());
			 System.out.println("from list(反向引用User)"+psn.getUser().getAccount());
		}
		
		
		
		
//		for (DictItem dictItem : cardlist) {
//			System.out.println("卡片："+dictItem.getName()+" / "+dictItem.getId());
//			UserSet ul=dictItem.getCardTypeUserSet();
//			for (User user : ul) {
//				System.out.println("\t"+user.getAccount());
//			}
//		}
//		
//		
//		

//			
//			
//		}
		
		
		
		
		
		return "ok";
	}
	
	@RequestMapping(value="/dict")
	public String dict() {
		
		//获得数据字典 一对多例子
		DictSet dicts=dao.query("select * from sys_dict").toEntitySet(DictSet.class);
		//关联字典条目
		dicts.joinDictItems();
		for (Dict dict : dicts) {
			System.out.println("数据字典："+dict.getName()+" / "+dict.getId());
			DictItemSet dictItemList=dict.getDictItems();
			for (DictItem dictItem : dictItemList) {
				System.out.println("\t"+dictItem.getId()+" = "+dictItem.getName()+",反向引用:  字典:"+dictItem.getOwnerDict().getName());
			}
		}
		
		return "ok";
	}
	
	@RequestMapping(value="/user_dict")
	public String dictUserDict() {
 
		UserSet users=dao.query("select * from sys_user where account like ? order by id asc","%na%").toEntitySet(UserSet.class);
		users.joinPerson();
		DictItemSet cardlist= users.joinDictItemOnCardType();
		for (DictItem dictItem : cardlist) {
			System.out.println("DICT ITEM "+dictItem.getName()+":::   "+dictItem.hashCode());
		}
		//DictItemList arealist= users.joinDictItemOnAreaType();
		//users.joinDictItemOnCardType();
		for (User user : users) { //循环user
			System.out.println(user.getPerson().getName()+"\t\t\t"+user.getAccount()+"\t\t\t"+user.getCardTypeDictItem().getName()+"::::   "+user.getCardTypeDictItem().hashCode());
//			System.out.println("from user join "+user.getPerson().getName()+",card="+user.getCardTypeDictItem().getName());
			//System.out.println("from user join "+user.getPerson().getName()+",card="+user.getCardTypeDictItem().getName()+",area="+user.getAreaTypeDictItem().getName());
		}
//		
//		System.out.println("========================");
//		
		for (DictItem cardType : cardlist) {
			System.out.println("使用 "+cardType.getName()+" 作为身份证明的人员有 ,"+cardType.hashCode());
			UserSet ul=cardType.getCardTypeUserSet();
			for (User uu : ul) {
				System.out.println("\t"+uu.getAccount()+","+uu.getPerson().getName()+","+uu.getCardType().hashCode());
			}
		}
		
		return "ok";
	}
	
	
	/**
	 * 多对多关联
	 * */
	@RequestMapping(value="/user_role")
	public String user_role() {
		
		//用户与角色的多对多关联
		UserSet users=dao.query("select * from sys_user where deleted=?  order by id asc","N").toEntitySet(UserSet.class);
		//users.joinPerson();
		RoleSet roles= users.joinRole();
		for (Role role : roles) {
			System.out.println("角色:"+role.getName());
			UserSet ul=role.getUserSet();
			System.out.println("get-1 "+role.hashCode()+","+role.getName()+",users:"+ul.size());
			for (User user : ul) {
				System.out.println("\t "+user.getAccount()+","+user.hashCode());
			}
		}
		
		for (User user : users) { 
			RoleSet usrRoleList=user.getRoleSet();
			if(usrRoleList.size()==0) continue;
			System.out.println("用户 "+user.getAccount()+"("+user.hashCode()+")"+",关联角色数："+usrRoleList.size());
			for (Role role : usrRoleList) {
				System.out.println("\t角色 : "+role.getName());
				UserSet ul=role.getUserSet();
				System.out.println("\t角色 : "+role.getName()+"反向关联的的用户数 : "+ul.size());
			}
		}
		
 
		return "ok";
	}
	
	@RequestMapping(value="/user_entity")
	public String entity() {
		
		//通过sample查询
		User u=new User();
		u.setId(50910678407712768l);
		User u2=dao.queryEntity(u); //  查询单个实体
		
		//
		u=new User();
		u.setAreaType("TR");
		UserSet us=dao.queryEntitySet(u); //查询实体集
		System.out.println(us.size());
		
		return "ok";
		
		
		
	}
}
